let FriendRequestModel = require('../model').friendRequestModel;
let DaoTool = require('../lib/daoTool');
exports.isExistFriendRequest = function (conn, MeOpenId, TaOpenId) {
    return new Promise(function (resolve, reject) {
        conn.query("select * from friend_request where MeOpenId=? and TaOpenId=?", [MeOpenId, TaOpenId], function (err, result) {
            printLog("=isExistFriendRequest=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};
exports.queryFriendRequest = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query("select * from friend_request where AddId=?", [params.AddId], function (err, result) {
            printLog("=queryFriendRequest=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};
exports.updateFriendRequest = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query("update friend_request set ? where AddId=?;", [DaoTool.generateModelObject(params, FriendRequestModel, 'update'),params.AddId], function (err, result) {
            printLog("=updateFriendRequest=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};
//创建好友请求
exports.createFriendRequest = function (conn, params) {
    return new Promise(function (resolve, reject) {
        let sql = "insert into friend_request set ?";
        conn.query(sql, [DaoTool.generateModelObject(params, FriendRequestModel, 'create')], function (err, result) {
            printLog("=createFriendRequest=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};
//删除好友请求
exports.deleteFriendRequest = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query("delete from friend_request where AddId=?;", [params.AddId], function (err, result) {
            printLog("=deleteFriendRequest=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};
//删除好友请求
exports.deleteAllFriendRequest = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query("delete from friend_request where (MeOpenId=? and TaOpenId=?) or (MeOpenId=? and TaOpenId=?);", [params.MeOpenId,params.TaOpenId,params.TaOpenId,params.MeOpenId,], function (err, result) {
            printLog("=deleteAllFriendRequest=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};
//已发送好友请求
exports.selectFriendRequestToTa = function (conn, params, isCount) {
    return new Promise(function (resolve, reject) {
        let sql = "from friend_request as fdr LEFT JOIN user as u on u.openid = fdr.TaOpenId left join my_option as myo on myo.openid=fdr.TaOpenId left join ta_option as tao on tao.openid = fdr.TaOpenId where " + getSelectFriendRequestWhere(params.params);
        let selectSql = "select fdr.AddId,fdr.MeOpenId,fdr.TaOpenId,fdr.StatusCode,fdr.CreatedAt as AddCreatedAt,u.*,myo.MyField147 as Birthday,myo.MyField148 as Height," + formatMatchingSqlStr(params.selfInfo) + sql + DaoTool.getPageParamsSqlStr(params.params);
        let countSql = "select count(1) as num " + sql;
        conn.query(selectSql, [], function (err, selectResult) {
            printLog("=selectFriendRequestToTa err=", err);
            printLog("=selectFriendRequestToTa=", this.sql);
            if (err) reject(err);
            if (!isCount) resolve(selectResult);
            conn.query(countSql, [], function (err, countResult) {
                printLog("=countFriendRequestToTa=", this.sql);
                console.log(countResult);
                if (err) reject(err);
                resolve({data: selectResult, count: countResult[0].num});
            });
        });
    });
};
//待确认好友请求
exports.selectFriendRequestToMe = function (conn, params, isCount) {
    return new Promise(function (resolve, reject) {
        let sql = " from friend_request as fdr LEFT JOIN user as u on u.openid = fdr.MeOpenId left join my_option as myo on myo.openid=fdr.MeOpenId left join ta_option as tao on tao.openid = fdr.MeOpenId where " + getSelectFriendRequestWhere(params.params);
        let selectSql = "select fdr.AddId,fdr.MeOpenId,fdr.TaOpenId,fdr.StatusCode,fdr.CreatedAt as AddCreatedAt,u.*,myo.MyField147 as Birthday," + formatMatchingSqlStr(params.selfInfo) + sql + DaoTool.getPageParamsSqlStr(params.params);
        let countSql = "select count(1) as num " + sql;
        conn.query(selectSql, [], function (err, selectResult) {
            printLog("=selectFriendRequestToMe=", this.sql);
            if (err) reject(err);
            if (!isCount) resolve(selectResult);
            conn.query(countSql, [], function (err, countResult) {
                if (err) reject(err);
                resolve({data: selectResult, count: countResult[0].num});
            });
        });
    });
};

function getSelectFriendRequestWhere(params) {
    let sql = "";
    sql += params.TaOpenId ? " and fdr.TaOpenId = '" + params.TaOpenId + "'" : "";
    sql += params.MeOpenId ? " and fdr.MeOpenId = '" + params.MeOpenId + "'" : "";
    sql += params.StatusCode ? " and fdr.StatusCode = '" + params.StatusCode + "'" : "";
    if (sql)
        sql = sql.substring(4);
    return sql;
}
function formatMatchingSqlStr(params) {
    try {
        let myo_sql = "";
        let tao_sql = "";
        for (let k = 0; k <= 68; k++) {
            let i = config.questionArr[k];
            if (!params["MyField" + i] || !params["TaField" + i])
                continue;
            if (myo_sql) {
                myo_sql += "+";
                tao_sql += "+";
            }
            myo_sql += '(case when myo.MyField' + i + ' is null then 0 else (matching(myo.MyField' + i + ',"' + params["TaField" + i] + '")) end)';
            tao_sql += '(case when tao.TaField' + i + ' is null then 0 else (matching("' + params["MyField" + i] + '",tao.TaField' + i + ')) end)';
        }
        myo_sql = "((" +(myo_sql?myo_sql:0)  + ")/68) as ToMe";
        tao_sql = "((" + (tao_sql?tao_sql:0) + ")/68) as ToTa ";
        return myo_sql + "," + tao_sql;
    } catch (err) {
        console.log(err);
    }
}